﻿using KamRemakeRemake.Common;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace KamRemakeRemake.GUI {
    /// <summary>
    /// The cursor, a singleton class
    /// </summary>
    public class Cursor : IDrawableGameObject {
        /// <summary>
        /// All possible cursor types
        /// </summary>
        public enum CursorType {
            Default,
            Select,
            ScrollUp,
            ScrollDown,
            ScrollLeft,
            ScrollRight,
            Join,
            Kill,
            Pen,
        }

        private static Cursor _instance;
        public static Cursor Instance {
            get {
                if (_instance == null) {
                    _instance = new Cursor();
                }
                return _instance;
            }
        }

        /// <summary>
        /// Private constructor because the cursor is a singleton
        /// </summary>
        private Cursor() {
        }

        private ContentManager contentManager;
        private Texture2D cursorTexture;
        public Vector2 Position { get; set; }

        public Rectangle ScreenRectangle {
            get {
                return new Rectangle(
                    (int)this.Position.X, 
                    (int)this.Position.Y, 
                    this.cursorTexture.Width, 
                    this.cursorTexture.Height);
            }
        }

        /// <summary>
        /// Sets the cursor to the given type
        /// </summary>
        public void SetCursor(CursorType cursorType) {
            this.cursorTexture = contentManager.Load<Texture2D>("sprites/cursor/" + cursorType.ToString().ToLower());
        }

        /// <summary>
        /// Saves the contentmanager for future use and sets the default cursor
        /// </summary>
        public void LoadContent(ContentManager contentManager) {
            this.contentManager = contentManager;
            this.SetCursor(CursorType.Default);
        }

        /// <summary>
        /// Updates the cursor to the current mouse position
        /// </summary>
        public void Update(GameTime gameTime) {
            this.Position = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
        }

        /// <summary>
        /// Draws the cursor
        /// </summary>
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, ContentManager contentManager) {
            spriteBatch.Draw(cursorTexture, Position, Color.White);
        }
    }
}
